package semant.first_pass;

import semant.Env;
import symbol.Symbol;
import symbol.ClassInfo;
import symbol.VarInfo;
import syntaxtree.VarDecl;
import syntaxtree.VisitorAdapter;

final class AttributeHandler extends VisitorAdapter {

    private Env env;
    private ClassInfo cinfo;

    private AttributeHandler(Env e, ClassInfo c){
	super();
	env = e;
	cinfo = c;
    }

    static void firstPass(Env e, ClassInfo c, VarDecl vd){
	AttributeHandler h = new AttributeHandler(e, c);
	vd.accept(h);
    }

    public void visit(VarDecl node){
    	Symbol name = Symbol.symbol(node.name.s);
    	VarInfo v = new VarInfo(node.type, name);

	// insere atributo na classe
    	if(!cinfo.addAttribute(v)){
	    // atributo ja existe
	    VarInfo old = cinfo.attributes.get(name);
	    env.err.Error(node.type, new Object[]{"Atributo \'" + name + "\' redeclarado para classe '" + cinfo.name + "'", "Declaracao anterior aqui: [" + old.type.line + "," + old.type.row + "]"});
    	}
    }

}
